package com.example.javatwo.service.cg.tzl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.javatwo.mapper.cg.tzl.PanDianMapper;
import com.example.javatwo.mapper.xs.dh.DsdssaleMainDao;
import com.example.javatwo.pojo.cg.lmx.Product;
import com.example.javatwo.pojo.cg.tzl.PanDian;
import com.example.javatwo.pojo.cg.tzl.PanDianXiang;
import com.example.javatwo.pojo.xs.dh.DsdssaleMain;
import com.example.javatwo.pojo.xs.dh.DsdssaleSub;
import com.example.javatwo.pojo.xs.zqy.paymentdetailed1;

@Service
public class PanDianService {
	
	
	@Autowired
	private PanDianMapper pandianmapper;
	
	//查询
	public List<PanDian> findZhuAll(int currentPage){
		List<PanDian> list = this.pandianmapper.findZhuAll(currentPage);
		PanDian pd = new PanDian();
		for (PanDian panDian : list) {
			pd.setPandianxiang(this.pandianmapper.findAll(panDian.getBillNo()));
		}
//		System.out.println("==================");
//		System.out.println(pd);
		list.add(pd);
		return list;
	}
	
	//新增
	public void insertzhu(PanDian pandian){
		this.pandianmapper.insertzhu(pandian);
		for (PanDianXiang xiang: pandian.getPandianxiang()) {
			xiang.setBillNo(pandian.getBillNo());
			this.pandianmapper.insertxiang(xiang);
		}
	}
	
	//修改
	public void update(PanDian pandian){
		this.pandianmapper.delXiang(pandian.getBillNo());
		
		this.pandianmapper.delZhu(pandian.getBillNo());
		
		this.pandianmapper.insertzhu(pandian);
		for (PanDianXiang xiang: pandian.getPandianxiang()) {
			xiang.setBillNo(pandian.getBillNo());
			this.pandianmapper.insertxiang(xiang);
		}
	}
	
	//删除
	public void del(String BillNo){
		System.out.println(BillNo);
			this.pandianmapper.delXiang(BillNo);
			this.pandianmapper.delZhu(BillNo);
			

		}
	
	public int findzong(){
		return this.pandianmapper.findzong();
	}
	//审核
	public void shenhe(PanDian p){
		pandianmapper.updatePanDian(p);
		for (PanDianXiang item : p.getPandianxiang()) {
			Product product = this.pandianmapper.findproduct(item.getProdID());
			if (product==null) {
				pandianmapper.insertProduct(item);
			}else{
				pandianmapper.updateProduct(item.getProdID(),item.getSumCheckQty());
			}
			
		}
	}
	//反审核
	public void unshenhe(PanDian p){
		pandianmapper.updatePanDian2(p);
		for (PanDianXiang item : p.getPandianxiang()) {
			pandianmapper.deleteProduct(item);
		}
	}
}
